package com.ruoyi.mixuebingcheng.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mixuebingcheng.domain.bo.ViewStarBo;
import com.ruoyi.mixuebingcheng.domain.vo.ViewStarVo;
import com.ruoyi.mixuebingcheng.domain.ViewStar;
import com.ruoyi.mixuebingcheng.mapper.ViewStarMapper;
import com.ruoyi.mixuebingcheng.service.IViewStarService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * VIEWService业务层处理
 *
 * @author ruoyi
 * @date 2023-10-27
 */
@RequiredArgsConstructor
@Service
public class ViewStarServiceImpl implements IViewStarService {

    private final ViewStarMapper baseMapper;

    /**
     * 查询VIEW
     */
    @Override
    public ViewStarVo queryById(Long starId){
        return baseMapper.selectVoById(starId);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public TableDataInfo<ViewStarVo> queryPageList(ViewStarBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ViewStar> lqw = buildQueryWrapper(bo);
        Page<ViewStarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询VIEW列表
     */
    @Override
    public List<ViewStarVo> queryList(ViewStarBo bo) {
        LambdaQueryWrapper<ViewStar> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ViewStar> buildQueryWrapper(ViewStarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ViewStar> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStarId() != null, ViewStar::getStarId, bo.getStarId());
        lqw.eq(bo.getGoodId() != null, ViewStar::getGoodId, bo.getGoodId());
        lqw.eq(bo.getUserId() != null, ViewStar::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), ViewStar::getName, bo.getName());
        lqw.eq(bo.getCategoryId() != null, ViewStar::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getPrice() != null, ViewStar::getPrice, bo.getPrice());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), ViewStar::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getImage()), ViewStar::getImage, bo.getImage());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), ViewStar::getDescription, bo.getDescription());
        lqw.eq(bo.getSort() != null, ViewStar::getSort, bo.getSort());
        lqw.eq(bo.getStore() != null, ViewStar::getStore, bo.getStore());
        lqw.eq(bo.getIsNew() != null, ViewStar::getIsNew, bo.getIsNew());
        lqw.eq(bo.getIsOnSale() != null, ViewStar::getIsOnSale, bo.getIsOnSale());
        lqw.eq(bo.getIsDeleted() != null, ViewStar::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增VIEW
     */
    @Override
    public Boolean insertByBo(ViewStarBo bo) {
        ViewStar add = BeanUtil.toBean(bo, ViewStar.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setStarId(add.getStarId());
        }
        return flag;
    }

    /**
     * 修改VIEW
     */
    @Override
    public Boolean updateByBo(ViewStarBo bo) {
        ViewStar update = BeanUtil.toBean(bo, ViewStar.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ViewStar entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除VIEW
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
